/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources.ui;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.TreeCellRenderer;


/**
 * Custom cell renderer for the JCheckBoxTree.
 *
 * @author Rogue
 */
public class JCheckBoxTreeCellRenderer extends JPanel
    implements TreeCellRenderer {
    private JCheckBox checkBox = null;
    private JLabel label = null;

    /**
     * Creates a new instance of JCheckBoxTreeCellRenderer
     */
    public JCheckBoxTreeCellRenderer() {
        checkBox = new JCheckBox();
        checkBox.setOpaque(false);
        label = new JLabel("Empty");

        setLayout(new BorderLayout());
        setOpaque(false);

        add(checkBox, BorderLayout.WEST);
        add(label, BorderLayout.EAST);
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
                                                  boolean selected,
                                                  boolean expanded,
                                                  boolean leaf, int row,
                                                  boolean hasFocus) {
        if (value instanceof JCheckBoxTreeNode) {
            JCheckBoxTreeNode node = (JCheckBoxTreeNode) value;
            label.setText(node.getUserObject());
            checkBox.setSelected(node.isSelected());
        } else {
            label.setText(value.toString());
            checkBox.setSelected(selected);
        }

        return this;
    }

    public boolean containsPointWithinCheckBox(int x, int y) {
        return checkBox.contains(x, y);
    }
}
